/*
 * Copyright (c) 2020. André Mion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.louvre;


import com.andremion.louvre.util.Utils;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;

/**
 * { AppCompatActivity} that manages request for { Manifest.permission#READ_EXTERNAL_STORAGE} Permission
 */
public abstract class StoragePermissionActivity extends Ability {
    private static final int REQUEST_READ_EXTERNAL_STORAGE = 4321;
    private static final int REQUEST_APP_SETTINGS = 1234;
    /**
     * RESULT_OK
     */
    public static final int RESULT_OK = 200;

    /**
     * askForPermission
     */
    public void askForPermission() {
        if (verifySelfPermission("ohos.permission.READ_MEDIA") != IBundleManager.PERMISSION_GRANTED) {
            requestPermissionsFromUser(
                    new String[]{"ohos.permission.READ_MEDIA"
                    }, REQUEST_READ_EXTERNAL_STORAGE);
        } else {
            onPermissionGranted();
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_READ_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                    onPermissionGranted();
                } else {
                    showExplanation();
                }
                break;
            }
            default:
        }
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        if (requestCode == REQUEST_APP_SETTINGS) {
            askForPermission();
        } else {
            super.onAbilityResult(requestCode, resultCode, resultData);
        }
    }

    /**
     * Callback when permission is granted
     */
    public abstract void onPermissionGranted();

    /**
     * Show UI with rationale for requesting a storage permission.
     * <p>
     * "You should do this only if you do not have the permission and the context in
     * which the permission is requested does not clearly communicate to the user
     * what would be the benefit from granting this permission."
     * </p>
     */
    private void showExplanation() {
        new ToastDialog(getContext()).setText(Utils.getString(getContext(),
                ResourceTable.String_activity_gallery_permission_request_explanation)).show();
    }
}